SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO




--凭证重置(1=财务收付，2=发票审核，3=贴现承兑，4=银行借款，5=银行还款,6=其他凭证,7=合并的发票凭证)
create PROCEDURE [dbo].[sp_jiuqi_voucher_rest]
	@logonid INT,
	@xxid INT,
	@xxtype INT,
	@errMsg VARCHAR(255) OUTPUT

AS

if dbo.gf_chk_right(1100084,@logonid)=0
BEGIN
	SELECT @errMsg='对不起,你无操作权限!'
	return
END

DECLARE @jiuqi_insert_flag INT,@jiuqi_delete_flag INT

if @xxtype=1
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag FROM t_cw_sfk WHERE sfkid=@xxid
ELSE if @xxtype=2
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag FROM t_cw_kp WHERE kpid=@xxid
ELSE if @xxtype=3
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag FROM t_cw_pj_bill WHERE billid=@xxid
ELSE if @xxtype=4
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag FROM t_cw_jk WHERE jkid=@xxid
ELSE if @xxtype=5
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag FROM t_cw_bank_dtl WHERE pkid=@xxid
ELSE if @xxtype=6
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag  FROM t_cw_sys_accvouch1 WHERE jl_acc1id=@xxid
ELSE if @xxtype=7
	SELECT @jiuqi_insert_flag=jiuqi_insert_flag,@jiuqi_delete_flag=jiuqi_delete_flag  FROM t_cw_kp_u8hb WHERE hbid=@xxid


SELECT @jiuqi_insert_flag=isnull(@jiuqi_insert_flag,0),@jiuqi_delete_flag=isnull(@jiuqi_delete_flag,0)
if @jiuqi_insert_flag<>2 AND @jiuqi_delete_flag<>2 OR @jiuqi_insert_flag=2 AND @jiuqi_delete_flag=2
BEGIN
	SELECT @errMsg='该凭证不符合重置条件!'
	return
END

BEGIN TRANSACTION
	if @xxtype=1 --1=财务收付
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_sfk SET jiuqi_insert_flag=1 WHERE sfkid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_sfk SET jiuqi_delete_flag=1 WHERE sfkid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END

		GOTO Finish
	END

	if @xxtype=2 --2=发票审核
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_kp SET jiuqi_insert_flag=1 WHERE kpid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_kp SET jiuqi_delete_flag=1 WHERE kpid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END
		GOTO Finish
	END

	if @xxtype=3 --3=贴现承兑
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_pj_bill SET jiuqi_insert_flag=1 WHERE billid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_pj_bill SET jiuqi_delete_flag=1 WHERE billid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END
		GOTO Finish
	END

	if @xxtype=4 --4=银行借款
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_jk SET jiuqi_insert_flag=1 WHERE jkid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_jk SET jiuqi_delete_flag=1 WHERE jkid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END
		GOTO Finish
	END

	if @xxtype=5 --5=银行还款
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_bank_dtl SET jiuqi_insert_flag=1 WHERE pkid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_bank_dtl SET jiuqi_delete_flag=1 WHERE pkid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END
		GOTO Finish
	END

	if @xxtype=6 --6=其他凭证
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_sys_accvouch1 SET jiuqi_insert_flag=1 WHERE jl_acc1id=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_sys_accvouch1 SET jiuqi_delete_flag=1 WHERE jl_acc1id=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END
		GOTO Finish
	END

	if @xxtype=7 --7=发票合并
	BEGIN
		if @jiuqi_insert_flag=2
			UPDATE t_cw_kp_u8hb SET jiuqi_insert_flag=1 WHERE hbid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		ELSE
			UPDATE t_cw_kp_u8hb SET jiuqi_delete_flag=1 WHERE hbid=@xxid AND isnull(jiuqi_insert_flag,0)=@jiuqi_insert_flag AND isnull(jiuqi_delete_flag,0)=@jiuqi_delete_flag
		if @@Error<>0 OR @@Rowcount<>1
		BEGIN
			SELECT @errMsg='更新凭证重置条件发生错误,操作失败!'
			GOTO Error
		END
		GOTO Finish
	END
Finish:

COMMIT TRANSACTION
return 0

Error:
ROLLBACK TRANSACTION
return 1







